package com.mission.schedule.CommonDialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.clock.QueryAlarmData;
import com.mission.schedule.constants.Const;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.sinaadd.BaseDialog;
import com.mission.schedule.utils.CalendarChange;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.MyLinearLayout;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.StringUtils;
import com.mission.schedule.utils.Utils;
import com.mission.schedule.view.CycleWheelView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日程内容编辑对话框
 * Created by liwei on 2017/4/18.
 */

public class MonthFragmentAddDialog extends Dialog {

    CallBack callBack;

    SharedPrefUtil sharedPrefUtil;
    Context context;
    WindowManager windowManager;
    EditText content;
    InputMethodManager imm;
    View view;
    TextView check_color, color1_tv, color2_tv, color3_tv, color4_tv, color5_tv, color6_tv,week_date,back;
    int check_colors[] = {R.drawable.month_button_color1, R.drawable.month_button_color2, R.drawable.month_button_color3
            , R.drawable.month_button_color4, R.drawable.month_button_color5, R.drawable.month_button_color6};
    LinearLayout colors_ll;
    int colors[] = {R.color.white,R.color.color_m_1,R.color.color_m_2,R.color.color_m_3,R.color.color_m_4,R.color.color_m_5};
    String date_str;
    RelativeLayout close;
    boolean isEd = false;
    int newopeartType = 0;

    public MonthFragmentAddDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager, final String date, final int newopeartType) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        this.newopeartType = newopeartType;

        setCanceledOnTouchOutside(true);

        view = LayoutInflater.from(context).inflate(R.layout.month_input_data, null);
        setContentView(view);

        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        date_str = date;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        content = view.findViewById(R.id.content);
        week_date = view.findViewById(R.id.week_date);
        check_color = view.findViewById(R.id.check_color);
        color1_tv = view.findViewById(R.id.color1_tv);
        color2_tv = view.findViewById(R.id.color2_tv);
        color3_tv = view.findViewById(R.id.color3_tv);
        color4_tv = view.findViewById(R.id.color4_tv);
        color5_tv = view.findViewById(R.id.color5_tv);
        color6_tv = view.findViewById(R.id.color6_tv);
        colors_ll = view.findViewById(R.id.colors_ll);
        back = view.findViewById(R.id.back);
        close = view.findViewById(R.id.close);

        week_date.setText(date.substring(5, date.length()) + " " + DateUtil.Week2(date) + " 全天");

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        color1_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check_color.setBackground(context.getResources().getDrawable(check_colors[0]));
                check_color.setTextColor(Color.BLACK);
                setContentColor(0);
            }
        });
        color2_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check_color.setBackground(context.getResources().getDrawable(check_colors[1]));
                check_color.setTextColor(Color.BLACK);
                setContentColor(1);
            }
        });
        color3_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check_color.setBackground(context.getResources().getDrawable(check_colors[2]));
                check_color.setTextColor(Color.BLACK);
                setContentColor(2);
            }
        });
        color4_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check_color.setBackground(context.getResources().getDrawable(check_colors[3]));
                check_color.setTextColor(Color.WHITE);
                setContentColor(3);
            }
        });
        color5_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check_color.setBackground(context.getResources().getDrawable(check_colors[4]));
                check_color.setTextColor(Color.WHITE);
                setContentColor(4);
            }
        });
        color6_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check_color.setBackground(context.getResources().getDrawable(check_colors[5]));
                check_color.setTextColor(Color.WHITE);
                setContentColor(5);
            }
        });


        content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (isEd) {
                    return;
                }
                isEd = true;
                setContentColor(color_position);
                isEd = false;
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        check_color.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                colors_ll.setVisibility(View.VISIBLE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                colors_ll.setVisibility(View.GONE);
                imm.showSoftInput(content, 0);
            }
        });

        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                colors_ll.setVisibility(View.GONE);
                imm.showSoftInput(content, 0);
            }
        });

        week_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CLOCKORDER, "0").equals("0")) {
                    dialogMorningTimeOnClick(week_date);
                } else {
                    initDiaLog(week_date);
                }
            }
        });

        imm.showSoftInput(content, 0);

    }

    @Override
    public void dismiss() {
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        super.dismiss();
    }

    int color_position = 0;

    private void setContentColor(int position) {
        color_position = position;
        if (color_position > 2) {
            content.setTextColor(Color.WHITE);
            color_position_f = "1";
        } else {
            content.setTextColor(Color.BLACK);
            color_position_f = "0";
        }
        String conten = content.getText().toString();
        StringBuffer buf = new StringBuffer(conten);
        SpannableStringBuilder style = new SpannableStringBuilder(buf.toString());
        //设置指定位置文字背景颜色
        style.setSpan(new BackgroundColorSpan(context.getResources().getColor(colors[color_position])), 0, conten.length(),
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        content.setText(style);
        content.setSelection(conten.length());
    }
    int schBeforeTime = 0;
    String schTime = "23:59";
    int schIsPostpone = 0;
    int schDisplayTime = 0;
    String color_position_f = "0";//1白色
    int isAlarm = 0;

    void save() {
        dismiss();
        if (!content.getText().toString().isEmpty()) {
            int schColocType = 0;
            int schImporttant = 0;
            switch (newopeartType) {
                case -2:
                    schImporttant = 1;
                    break;
                case -3:
                    schIsPostpone = 1;
                    break;
                case -6:
                case -7:
                case -8:
                case -9:
                case -10:
                case -11:
                case -12:
                    schColocType = Math.abs(newopeartType) - 5;
                    break;
            }
            if (newopeartType == -4) {
                App.getDBcApplication().insertScheduleData(content.getText().toString() + " (到期日 " + date_str+ ")"
                        , date_str, schTime, 1,
                        0, 1, 0, 0,
                        0, 0,date_str + " " + schTime +":00", "",
                        2, DateUtil.nowTime2(), "0", 0, "", "",
                        1, 0, 0, "到点提醒", "g_001", "", 0, 0, 0, "", "", 0, 0, -1, 0, "0",color_position_f ,color_position+"");
            } else {
                App.getDBcApplication().insertScheduleData(content.getText().toString(),
                        date_str, schTime, isAlarm, schBeforeTime,
                        schDisplayTime, schIsPostpone, schImporttant, schColocType, 0,
                        date_str + " " + schTime +":00", "",
                        2, DateUtil.nowTime2(), "0", 0, "", "",
                        1, 0, 0, "完成任务", "g_88", "", 0, 0, 0, "", "", 0, 0, 0, 0, "0",color_position_f ,color_position+"");
            }
            QueryAlarmData.writeAlarm(context);
            Toast.makeText(context, "新建成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Const.SHUAXINDATA);
            intent.putExtra("data", "success");
            intent.putExtra("index", 1);
            intent.putExtra("what", 2);
            context.sendBroadcast(intent);
            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.IS_MONTH_RE, "1");
            callBack.reminderEditTime();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        dismiss();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /* 触摸外部弹窗 */
        if (isOutOfBounds(getContext(), event)) {
            dismiss();
            return false;
        }
        return super.onTouchEvent(event);
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void reminderEditTime();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    private void initDiaLog(TextView time_tv) {
        final BaseDialog dialog = new BaseDialog(context, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
//        params.alpha = 0.92f;
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(context);
        View more_pop_menu = fac.inflate(R.layout.dialog_calender_time, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = windowManager.getDefaultDisplay().getWidth();
        dialog.show();
        dialog.setCallBack(new BaseDialog.CallBack() {
            @Override
            public void onTouchOutside() {
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CLOCKORDER, "1");
                dialog.dismiss();
            }
        });

        new MyClick(dialog, more_pop_menu, time_tv);
    }

    private Animation animationTranslate;
    private RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(0, 0);

    // 移动的动画效果
    /*
     * TranslateAnimation(float fromXDelta, float toXDelta, float
     * fromYDelta,float toYDelta)
     *
     * float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值；
     *
     * float toXDelta, 这个参数表示动画结束的点离当前View X坐标上的差值；
     *
     * float fromYDelta, 这个参数表示动画开始的点离当前View Y坐标上的差值；
     *
     * float toYDelta)这个参数表示动画开始的点离当前View Y坐标上的差值；
     */
    protected Animation animTranslate(float toX, float toY, final int x1,
                                      final int y1, final TextView textView, long durationMillis) {
        animationTranslate = new TranslateAnimation(toX, toX, toY, toY);
        animationTranslate.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                params = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(x1, y1, 0, 0);
                textView.setLayoutParams(params);
                textView.setPadding(12, 7, 12, 7);
                textView.clearAnimation();
            }
        });
        animationTranslate.setDuration(durationMillis);
        return animationTranslate;
    }

    class MyClick implements View.OnClickListener {

        private Dialog dialog;
        private RelativeLayout rela_hour;
        private RelativeLayout rela_minutes;
        private int hours = 12;
        private int minutes = 0;
        private int width;
        private int centerX;
        private int centerY;
        private Calendar calendar;
        private int lastMinutes;
        private TextView dialog_tv_state, dialog_tv_state1;
        private int grState;// 结果为0是上午 结果为1是下午
        private String chooseHour;
        private String chooseMinute;

        private TextView tv_clock_state;
        private LinearLayout ll_clock, state_ll;
        private TextView tv_clock_time;
        private TextView tv_clock_beforetime;
        private TextView time_tv, tv_gt;

        private RelativeLayout date_re;
        String time;

        private MyClick(Dialog dialog, View more_pop_menu, TextView time_tvs) {
            this.dialog = dialog;
            this.time_tv = time_tvs;
            time = schTime;
            calendar = Calendar.getInstance();
            // if (isNeedPush) {
            String[] times;

            if ("全天".equals(time)) {
                times = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ALLTIME, "08:58").split(":");
            } else {
                times = time.split(":");
            }
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
            calendar.set(Calendar.MINUTE, Integer.parseInt(times[1]));
            // }
            rela_hour = (RelativeLayout) more_pop_menu
                    .findViewById(R.id.rela_hour);
            rela_minutes = (RelativeLayout) more_pop_menu
                    .findViewById(R.id.rela_minutes);

            dialog_tv_state = (TextView) more_pop_menu
                    .findViewById(R.id.dialog_tv_state);
            date_re = (RelativeLayout) more_pop_menu
                    .findViewById(R.id.date_re);
            dialog_tv_state1 = (TextView) more_pop_menu
                    .findViewById(R.id.dialog_tv_state1);
            state_ll = (LinearLayout) more_pop_menu
                    .findViewById(R.id.state_ll);
            state_ll.setOnClickListener(this);
            tv_clock_state = (TextView) more_pop_menu
                    .findViewById(R.id.tv_clock_state);
            tv_clock_state.setOnClickListener(this);

            tv_gt = (TextView) more_pop_menu.findViewById(R.id.tv_gt);
            tv_gt.setOnClickListener(this);
            ll_clock = (LinearLayout) more_pop_menu.findViewById(R.id.ll_clock);
            ll_clock.setOnClickListener(this);

            tv_clock_time = (TextView) more_pop_menu.findViewById(R.id.tv_clock_time);
            tv_clock_beforetime = (TextView) more_pop_menu.findViewById(R.id.tv_clock_beforetime);

            width = windowManager.getDefaultDisplay().getWidth();
            int haf = Utils.dipTopx(context, 20);
            centerX = width / 2 - Utils.dipTopx(context, 16);
            centerY = width / 2 - Utils.dipTopx(context, 16) - haf;

            initclockValue();
            initHour();
            initMinutes();

            grState = calendar.get(Calendar.AM_PM);
//            if (grState == 0) {// 结果为0是上午 结果为1是下午
//                dialog_tv_state.setText("上午");
//            } else {
//                dialog_tv_state.setText("下午");
//            }
            if (grState == 1) {// 上午
                dialog_tv_state1.setTextColor(Color.parseColor("#08f0ff"));
                dialog_tv_state1.setTextSize(30);
                dialog_tv_state.setTextColor(Color.parseColor("#aaaaaa"));
                dialog_tv_state.setTextSize(14);
                grState = 1;
                calendar.set(Calendar.AM_PM, grState);
            } else if (grState == 0) {// 下午
                dialog_tv_state.setTextColor(Color.parseColor("#08f0ff"));
                dialog_tv_state.setTextSize(30);
                dialog_tv_state1.setTextColor(Color.parseColor("#aaaaaa"));
                dialog_tv_state1.setTextSize(14);
                grState = 0;
                calendar.set(Calendar.AM_PM, grState);
            }
        }

        private void initclockValue() {
            tv_clock_time.setText(time);
            if (time.split(":")[0].substring(0, 1).equals("0")) {
                if (Integer.valueOf(time.split(":")[0].substring(1, time.split(":")[0].length())) >= 0 &&
                        Integer.valueOf(time.split(":")[0].substring(1, time.split(":")[0].length())) <= 6) {
                    tv_clock_time.setText("凌晨 " + time);
                }
            }
        }

        private void initHour() {
            int hour = calendar.get(Calendar.HOUR);
            if (hour == 0) {
                hour = 12;
            }
            int hourRadius = Utils.dipTopx(context, 90); // 设置圆半径
            for (int i = 12; i >= 1; i--) {
                int dy = (int) (hourRadius * Math.cos((Math.PI / 6.0) * i));
                int dx = (int) (hourRadius * Math.sin((Math.PI / 6.0) * i));

                int x1 = centerX + dx;
                int y1 = centerY - dy;

                String text = hours < 10 ? "0" + hours : "" + hours;

                TextView textView = new TextView(context);
                textView.setText(text);
                textView.setTextColor(Color.WHITE);
                textView.setTextSize(18.0f);
                textView.setGravity(Gravity.CENTER);
                textView.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                textView.setTag(i);
                textView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        chooseHour = v.getTag().toString();
                        chageHour(Integer.parseInt(chooseHour));
                        calendar.set(Calendar.HOUR,
                                Integer.parseInt(chooseHour));
                        calendar.set(Calendar.MINUTE,
                                Integer.parseInt(chooseMinute));
                        if (grState == 0) {// 上午
                            calendar.set(Calendar.AM_PM, 0);
                        } else if (grState == 1) {// 下午
                            calendar.set(Calendar.AM_PM, 1);
                        }
                        String calendarHour = calendar
                                .get(Calendar.HOUR_OF_DAY) < 10 ? "0"
                                + calendar.get(Calendar.HOUR_OF_DAY) : ""
                                + calendar.get(Calendar.HOUR_OF_DAY);
                        String calendarMinute = calendar.get(Calendar.MINUTE) < 10 ? "0"
                                + calendar.get(Calendar.MINUTE)
                                : "" + calendar.get(Calendar.MINUTE);
                        schDisplayTime = 1;
                        isAlarm = 1;
                        schTime = calendarHour + ":" + calendarMinute;
                        week_date.setText(date_str + " " + DateUtil.Week2(date_str) + " "+schTime);
                        tv_clock_time.setText(((0 <= calendar
                                .get(Calendar.HOUR_OF_DAY) && calendar
                                .get(Calendar.HOUR_OF_DAY) <= 6) ? "凌晨" : "") + calendarHour + ":" + calendarMinute);
                    }
                });

                if (hour == i) {
                    if (time.equals("全天")) {
                        textView.setBackgroundDrawable(null);
                    } else {
                        chooseHour = text;
                        textView.setTextColor(Color.WHITE);
                        textView.setBackgroundResource(R.mipmap.icon_shuzi1);
                    }
                } else {
                    textView.setBackgroundDrawable(null);
                }

                rela_hour.addView(textView);

                textView.setAnimation(animTranslate(centerX, centerY, x1, y1, textView, 200));

                hours--;
            }
        }

        private void initMinutes() {
            int minute = calendar.get(Calendar.MINUTE);
            int result = minute % 5;
            if (result != 0) {
                int division = minute / 5;
                minute = division * 5;
            }
            int minutesRadius = Utils.dipTopx(context, 140); // 设置圆半径
            for (int i = 0; i < 12; i++) {
                int dy = (int) (minutesRadius * Math.cos((Math.PI / 6.0) * i));
                int dx = (int) (minutesRadius * Math.sin((Math.PI / 6.0) * i));

                int x1 = centerX + dx;
                int y1 = centerY - dy;

                String text = minutes * 5 < 10 ? "0" + minutes * 5 : "" + minutes * 5;

                TextView textView = new TextView(context);
                textView.setText(text);
                textView.setTextColor(Color.WHITE);
                textView.setTextSize(18.0f);
                textView.setGravity(Gravity.CENTER);
                textView.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                textView.setTag(text);
                textView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        chooseMinute = v.getTag().toString();
                        chageMinutes(Integer.parseInt(chooseMinute) / 5);
                        lastMinutes = Integer.parseInt(chooseMinute) / 5;
                        calendar.set(Calendar.HOUR,
                                Integer.parseInt(chooseHour));
                        calendar.set(Calendar.MINUTE,
                                Integer.parseInt(chooseMinute));
                        if (grState == 0) {// 上午
                            calendar.set(Calendar.AM_PM, 0);
                        } else if (grState == 1) {// 下午
                            calendar.set(Calendar.AM_PM, 1);
                        }
                        String calendarHour = calendar
                                .get(Calendar.HOUR_OF_DAY) < 10 ? "0"
                                + calendar.get(Calendar.HOUR_OF_DAY) : ""
                                + calendar.get(Calendar.HOUR_OF_DAY);
                        String calendarMinute = calendar.get(Calendar.MINUTE) < 10 ? "0"
                                + calendar.get(Calendar.MINUTE)
                                : "" + calendar.get(Calendar.MINUTE);
                        schDisplayTime = 1;
                        isAlarm = 1;
                        schTime = calendarHour + ":" + calendarMinute;
                        week_date.setText(date_str + " " + DateUtil.Week2(date_str) + " "+schTime);
                        tv_clock_time.setText(((0 <= calendar
                                .get(Calendar.HOUR_OF_DAY) && calendar
                                .get(Calendar.HOUR_OF_DAY) <= 6) ? "凌晨" : "") + calendarHour + ":" + calendarMinute);
                    }
                });
                if (minute == Integer.parseInt(text)) {
                    if (time.equals("全天")) {
                        textView.setBackgroundDrawable(null);
                    } else {
                        lastMinutes = i;
                        chooseMinute = text;
                        textView.setTextColor(Color.WHITE);
                        textView.setBackgroundResource(R.mipmap.icon_shuzi2);
                    }
                } else {
                    textView.setBackgroundDrawable(null);
                }
                rela_minutes.addView(textView);
                textView.setAnimation(animTranslate(centerX, centerY, x1, y1, textView, 200));

                minutes++;
            }
        }

        private void chageHour(int hours) {
            for (int i = rela_hour.getChildCount() - 1; i >= 0; i--) {
                TextView tv_hour = (TextView) rela_hour.getChildAt(i);
                if (hours == Integer.parseInt(tv_hour.getTag().toString())) {
                    tv_hour.setBackgroundResource(R.mipmap.icon_shuzi1);
                } else {
                    tv_hour.setBackgroundDrawable(null);
                }
            }
        }

        private void chageMinutes(int minutes) {
            TextView textView = (TextView) rela_minutes.getChildAt(lastMinutes);
            textView.setBackgroundDrawable(null);
            TextView tv_minutes = (TextView) rela_minutes.getChildAt(minutes);
            tv_minutes.setBackgroundResource(R.mipmap.icon_shuzi2);
        }


        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_gt:
                    dialogMorningTimeOnClick(time_tv);
                    dialog.dismiss();
                    break;
                case R.id.state_ll:
                    calendar.set(Calendar.HOUR, Integer.parseInt(chooseHour));
                    calendar.set(Calendar.MINUTE, Integer.parseInt(chooseMinute));
                    if (grState == 0) {// 上午
                        dialog_tv_state1.setTextColor(Color.parseColor("#08f0ff"));
                        dialog_tv_state1.setTextSize(30);
                        dialog_tv_state.setTextColor(Color.parseColor("#aaaaaa"));
                        dialog_tv_state.setTextSize(14);
                        grState = 1;
                        calendar.set(Calendar.AM_PM, grState);
                    } else if (grState == 1) {// 下午
                        dialog_tv_state.setTextColor(Color.parseColor("#08f0ff"));
                        dialog_tv_state.setTextSize(30);
                        dialog_tv_state1.setTextColor(Color.parseColor("#aaaaaa"));
                        dialog_tv_state1.setTextSize(14);
                        grState = 0;
                        calendar.set(Calendar.AM_PM, grState);
                    }
//                    dialog_tv_state.startAnimation(AnimationUtils.loadAnimation(context, R.anim.scale_in));
                    String calendarHour = calendar.get(Calendar.HOUR_OF_DAY) < 10 ? "0"
                            + calendar.get(Calendar.HOUR_OF_DAY)
                            : "" + calendar.get(Calendar.HOUR_OF_DAY);
                    String calendarMinute = calendar.get(Calendar.MINUTE) < 10 ? "0"
                            + calendar.get(Calendar.MINUTE)
                            : "" + calendar.get(Calendar.MINUTE);
                    tv_clock_time.setText(((0 <= calendar.get(Calendar.HOUR_OF_DAY) && calendar
                            .get(Calendar.HOUR_OF_DAY) <= 6) ? "凌晨" : "") + calendarHour + ":" + calendarMinute);
                    schTime = calendarHour + ":" + calendarMinute;
                    schDisplayTime = 1;
                    isAlarm = 1;
                    week_date.setText(date_str + " " + DateUtil.Week2(date_str) + " "+schTime);
//                    dialog.dismiss();
                    break;

                case R.id.tv_clock_state:
                    // isNeedPush = false;
                    week_date.setText(date_str + " " + DateUtil.Week2(date_str) + " 全天");
                    schDisplayTime = 0;
                    isAlarm = 0;
                    // tv_beforetime.setText("");
//                    dialog.dismiss();
                    break;

                case R.id.ll_clock:
//                    if (date_re.getVisibility() == View.VISIBLE){
//                        date_re.setVisibility(View.GONE);
//                        time_ll.setVisibility(View.VISIBLE);
//                    }else {
//                        date_re.setVisibility(View.VISIBLE);
//                        time_ll.setVisibility(View.GONE);
//                    }
                    break;

                default:
                    dialog.dismiss();

                    break;
            }
        }

    }

    int[] solartime = new int[25];
    boolean isLeap = false;
    boolean isRed = false;
    ArrayList<HashMap<String, String>> data;
    String today;
    int temp_day;
    int temp_month;
    int temp_year;

    int to_day;
    int to_month;
    int to_year;
    int standard_day;
    int standard_month;
    int standard_year;
    String holiday = "";
    String curDate = "";

    int sourse;//
    String dateTime = "";// 日期
    private TextView title;

    MyLinearLayout dataLinear;
    Handler handlerrili;
    LinearLayout.LayoutParams mLayoutParamss = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    private void initRiliDiaLog(final boolean isCopy, final String meesage) {
        final Dialog dialog = new Dialog(context, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(context);
        View more_pop_menu = fac.inflate(R.layout.dialog_calender_rili, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = windowManager.getDefaultDisplay().getWidth();
        dialog.show();

        curDate = dateTime;

        dataLinear = (MyLinearLayout) more_pop_menu.findViewById(R.id.datalinear);
        title = (TextView) more_pop_menu.findViewById(R.id.title);
        dataLinear.setBackgroundColor(Color.WHITE);
        more_pop_menu.findViewById(R.id.today).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                curDate = DateUtil.formatDate(new Date());// new Date()为获取当前系统时间
                GenData(true, curDate);
                GenView();
            }
        });
        more_pop_menu.findViewById(R.id.lastmonth).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (standard_month == 1) {
                    standard_month = 12;
                    standard_year--;
                } else {
                    standard_month--;
                }
                GenData(false, curDate);
                GenView();
                title.setText(standard_year + "年" + standard_month + "月");
                dataLinear.startAnimation(AnimationUtils.loadAnimation(context, R.anim.right_in));

            }
        });
        more_pop_menu.findViewById(R.id.nextmonth).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (standard_month == 12) {
                    standard_month = 1;
                    standard_year++;
                } else {
                    standard_month++;
                }
                GenData(false, curDate);
                GenView();
                title.setText(standard_year + "年" + standard_month + "月");
                dataLinear.startAnimation(AnimationUtils.loadAnimation(context, R.anim.left_in));

            }
        });
        handlerrili = new Handler() {
            public void handleMessage(Message msg) {
                if (msg == null) {
                    return;
                }
                switch (msg.what) {
                    case 0:
                        if (standard_month == 1) {
                            standard_month = 12;
                            standard_year--;
                        } else {
                            standard_month--;
                        }
                        GenData(false, curDate);
                        GenView();
                        title.setText(standard_year + "年" + standard_month + "月");
                        dataLinear.startAnimation(AnimationUtils.loadAnimation(context, R.anim.right_in));
                        break;

                    case 1:
                        if (standard_month == 12) {
                            standard_month = 1;
                            standard_year++;
                        } else {
                            standard_month++;
                        }
                        GenData(false, curDate);
                        GenView();
                        title.setText(standard_year + "年" + standard_month + "月");
                        dataLinear.startAnimation(AnimationUtils.loadAnimation(context, R.anim.left_in));
                        break;

                    case 2:
                        standard_year--;
                        GenData(false, curDate);
                        GenView();
                        title.setText(standard_year + "年" + standard_month + "月");
                        dataLinear.startAnimation(AnimationUtils.loadAnimation(context, R.anim.down_in));
                        break;

                    case 3:
                        standard_year++;
                        GenData(false, curDate);
                        GenView();
                        title.setText(standard_year + "年" + standard_month + "月");
                        dataLinear.startAnimation(AnimationUtils.loadAnimation(context, R.anim.up_in));
                        break;

                    case 9:
                        dialog.dismiss();
//                            calender_date.setText(msg.obj.toString());
//                            calender_week.setText(Week(msg.obj.toString()));

                        break;

                    case 11:
                        break;

                    case 12:
                        break;
                }
            }
        };
        dataLinear.setHandler(handlerrili);


        new LoadDataAsyncTask().execute();


    }

    class LoadDataAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
//			dialog.dismiss();
            GenData(true, curDate);
            GenView();
        }

    }

    public ArrayList<HashMap<String, String>> GenData(boolean isfirst, String date) {
        if (!date.equals("") && isfirst) {
            data = new ArrayList<HashMap<String, String>>();
            to_year = Integer.parseInt(date.split("-")[0].toString());
            to_month = Integer.parseInt(date.split("-")[1].toString());
            to_day = Integer.parseInt(date.split("-")[2].toString());
            temp_day = to_day;
            temp_month = to_month;
            temp_year = to_year;
            standard_day = to_day;
            standard_month = to_month;
            standard_year = to_year;
            today = date;
            title.setText(standard_year + "年" + standard_month + "月");
        } else {
            if (isfirst) {
                data = new ArrayList<HashMap<String, String>>();
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(System.currentTimeMillis());
                to_year = c.get(Calendar.YEAR);
                to_month = c.get(Calendar.MONTH);
                to_day = c.get(Calendar.DAY_OF_MONTH);

                to_month++;
                temp_day = to_day;
                temp_month = to_month;
                temp_year = to_year;
                standard_day = to_day;
                standard_month = to_month;
                standard_year = to_year;

                today = to_year + "-"
                        + (to_month > 9 ? "" + to_month : "0" + to_month) + "-"
                        + (to_day > 9 ? "" + to_day : "0" + to_day);
                title.setText(standard_year + "年" + standard_month + "月");
            } else {
                data = new ArrayList<HashMap<String, String>>();
                temp_day = standard_day;
                temp_month = standard_month;
                temp_year = standard_year;

            }
        }
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("day", temp_day + "");

        map.put("isMonth", "1"); // 1是本月
        String tempStrT = temp_year + "-"
                + (temp_month > 9 ? "" + temp_month : "0" + temp_month) + "-"
                + (temp_day > 9 ? "" + temp_day : "0" + temp_day);
        if ("".equals(StringUtils.getIsStringEqulesNull(tempStrT))) {
            tempStrT = today;
        }
        if (today.equals(tempStrT)) {
            map.put("isToday", "1"); // 1是今天
            map.put("time", today);
        } else {
            map.put("isToday", "0"); // 1是今天
            map.put("time", tempStrT);
        }
        try {
            if (CalHoliday(Integer.valueOf(temp_month
                    + (temp_day > 9 ? "" + temp_day : "0" + temp_day)))) {
                map.put("lunar", holiday);
            } else {
                map.put("lunar", CalendarChange.CalendarChange(tempStrT));
            }
            if (isRed) {
                map.put("isHoliday", "1"); // 1节日
                isRed = false;
            } else {
                map.put("isHoliday", "0"); // 1节日
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        data.add(map);
        for (int i = temp_day - 1; i > 0; i--) {
            String tempStr = temp_year + "-"
                    + (temp_month > 9 ? "" + temp_month : "0" + temp_month)
                    + "-" + (i > 9 ? "" + i : "0" + i);
            map = new HashMap<String, String>();
            map.put("day", i + "");
            map.put("time", tempStr);
            map.put("isToday", "0"); // 1是今天
            map.put("isMonth", "1"); // 1是本月
            try {
                if (CalHoliday(Integer.valueOf("" + temp_month
                        + (i > 9 ? "" + i : "0" + i)))) {
                    map.put("lunar", holiday);
                } else {
                    map.put("lunar", CalendarChange.CalendarChange(tempStr));
                }
                if (isRed) {
                    map.put("isHoliday", "1"); // 1节日
                    isRed = false;
                } else {
                    map.put("isHoliday", "0"); // 1节日
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            data.add(0, map);
        }
        int te_week = calWeek(temp_year, temp_month, 1);
        if (temp_month == 1) {
            temp_year--;
            temp_month = 12;
        } else {
            temp_month--;
        }
        calLeapYear(temp_year);
        if (temp_month == 4 || temp_month == 6 || temp_month == 9
                || temp_month == 11) {
            for (int i = 30; i > 30 - te_week; i--) {
                String tempStr = temp_year + "-"
                        + (temp_month > 9 ? "" + temp_month : "0" + temp_month)
                        + "-" + (i > 9 ? "" + i : "0" + i);
                map = new HashMap<String, String>();
                map.put("day", i + "");
                map.put("time", tempStr);
                map.put("isToday", "0"); // 是今天
                map.put("isMonth", "0"); // 1是本月
                try {
                    if (CalHoliday(Integer.valueOf("" + temp_month
                            + (i > 9 ? "" + i : "0" + i)))) {
                        map.put("lunar", holiday);
                    } else {
                        map.put("lunar", CalendarChange.CalendarChange(tempStr));
                    }
                    if (isRed) {
                        map.put("isHoliday", "1"); // 1节日
                        isRed = false;
                    } else {
                        map.put("isHoliday", "0"); // 1节日
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                data.add(0, map);
            }
        } else if (temp_month == 2) {
            if (isLeap) {
                for (int i = 29; i > 29 - te_week; i--) {
                    String tempStr = temp_year
                            + "-"
                            + (temp_month > 9 ? "" + temp_month : "0"
                            + temp_month) + "-"
                            + (i > 9 ? "" + i : "0" + i);
                    map = new HashMap<String, String>();
                    map.put("day", i + "");
                    map.put("time", tempStr);
                    map.put("isToday", "0"); // 1是今天
                    map.put("isMonth", "0"); // 1是本月
                    try {
                        if (CalHoliday(Integer.valueOf("" + temp_month
                                + (i > 9 ? "" + i : "0" + i)))) {
                            map.put("lunar", holiday);
                        } else {
                            map.put("lunar",
                                    CalendarChange.CalendarChange(tempStr));
                        }
                        if (isRed) {
                            map.put("isHoliday", "1"); // 1节日
                            isRed = false;
                        } else {
                            map.put("isHoliday", "0"); // 1节日
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    data.add(0, map);
                }
            } else {
                for (int i = 28; i > 28 - te_week; i--) {
                    String tempStr = temp_year
                            + "-"
                            + (temp_month > 9 ? "" + temp_month : "0"
                            + temp_month) + "-"
                            + (i > 9 ? "" + i : "0" + i);
                    map = new HashMap<String, String>();
                    map.put("day", i + "");
                    map.put("time", tempStr);
                    map.put("isToday", "0"); // 1是今天
                    map.put("isMonth", "0"); // 1是本月
                    try {
                        if (CalHoliday(Integer.valueOf("" + temp_month
                                + (i > 9 ? "" + i : "0" + i)))) {
                            map.put("lunar", holiday);
                        } else {
                            map.put("lunar",
                                    CalendarChange.CalendarChange(tempStr));
                        }
                        if (isRed) {
                            map.put("isHoliday", "1"); // 1节日
                            isRed = false;
                        } else {
                            map.put("isHoliday", "0"); // 1节日
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    data.add(0, map);
                }
            }
        } else {
            for (int i = 31; i > 31 - te_week; i--) {
                String tempStr = temp_year + "-"
                        + (temp_month > 9 ? "" + temp_month : "0" + temp_month)
                        + "-" + (i > 9 ? "" + i : "0" + i);
                map = new HashMap<String, String>();
                map.put("day", i + "");
                map.put("time", tempStr);
                map.put("isToday", "0"); // 1是今天
                map.put("isMonth", "0"); // 1是本月
                try {
                    if (CalHoliday(Integer.valueOf("" + temp_month
                            + (i > 9 ? "" + i : "0" + i)))) {
                        map.put("lunar", holiday);
                    } else {
                        map.put("lunar", CalendarChange.CalendarChange(tempStr));
                    }
                    if (isRed) {
                        map.put("isHoliday", "1"); // 1节日
                        isRed = false;
                    } else {
                        map.put("isHoliday", "0"); // 1节日
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                data.add(0, map);
            }
        }
        if (temp_month == 12) {
            temp_year++;
            temp_month = 1;
        } else {
            temp_month = temp_month + 1;
        }
        calLeapYear(temp_year);
        if (temp_month == 4 || temp_month == 6 || temp_month == 9
                || temp_month == 11) {
            for (int i = to_day + 1; i <= 30; i++) {
                String tempStr = temp_year + "-"
                        + (temp_month > 9 ? "" + temp_month : "0" + temp_month)
                        + "-" + (i > 9 ? "" + i : "0" + i);
                map = new HashMap<String, String>();
                map.put("day", i + "");
                map.put("time", tempStr);
                map.put("isToday", "0"); // 1是今天
                map.put("isMonth", "1"); // 1是本月
                try {
                    if (CalHoliday(Integer.valueOf("" + temp_month
                            + (i > 9 ? "" + i : "0" + i)))) {
                        map.put("lunar", holiday);
                    } else {
                        map.put("lunar", CalendarChange.CalendarChange(tempStr));
                    }
                    if (isRed) {
                        map.put("isHoliday", "1"); // 1节日
                        isRed = false;
                    } else {
                        map.put("isHoliday", "0"); // 1节日
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                data.add(map);
            }
        } else if (temp_month == 2) {
            if (isLeap) {
                for (int i = to_day + 1; i <= 29; i++) {
                    String tempStr = temp_year
                            + "-"
                            + (temp_month > 9 ? "" + temp_month : "0"
                            + temp_month) + "-"
                            + (i > 9 ? "" + i : "0" + i);
                    map = new HashMap<String, String>();
                    map.put("day", i + "");
                    map.put("time", tempStr);
                    map.put("isToday", "0"); // 是今天
                    map.put("isMonth", "1"); // 1是本月
                    try {
                        if (CalHoliday(Integer.valueOf("" + temp_month
                                + (i > 9 ? "" + i : "0" + i)))) {
                            map.put("lunar", holiday);
                        } else {
                            map.put("lunar",
                                    CalendarChange.CalendarChange(tempStr));
                        }
                        if (isRed) {
                            map.put("isHoliday", "1"); // 1节日
                            isRed = false;
                        } else {
                            map.put("isHoliday", "0"); // 1节日
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    data.add(map);
                }
            } else {
                for (int i = to_day + 1; i <= 28; i++) {
                    String tempStr = temp_year
                            + "-"
                            + (temp_month > 9 ? "" + temp_month : "0"
                            + temp_month) + "-"
                            + (i > 9 ? "" + i : "0" + to_day);
                    map = new HashMap<String, String>();
                    map.put("day", i + "");
                    map.put("time", tempStr);
                    map.put("isToday", "0"); // 是今天
                    map.put("isMonth", "1"); // 1是本月
                    try {
                        if (CalHoliday(Integer.valueOf("" + temp_month
                                + (i > 9 ? "" + i : "0" + i)))) {
                            map.put("lunar", holiday);
                        } else {
                            map.put("lunar",
                                    CalendarChange.CalendarChange(tempStr));
                        }
                        if (isRed) {
                            map.put("isHoliday", "1"); // 1节日
                            isRed = false;
                        } else {
                            map.put("isHoliday", "0"); // 1节日
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    data.add(map);
                }
            }
        } else {
            for (int i = to_day + 1; i <= 31; i++) {
                String tempStr = temp_year + "-"
                        + (temp_month > 9 ? "" + temp_month : "0" + temp_month)
                        + "-" + (i > 9 ? "" + i : "0" + i);
                map = new HashMap<String, String>();
                map.put("day", i + "");
                map.put("time", tempStr);
                map.put("isToday", "0"); // 是今天
                map.put("isMonth", "1"); // 1是本月
                try {
                    if (CalHoliday(Integer.valueOf("" + temp_month
                            + (i > 9 ? "" + i : "0" + i)))) {
                        map.put("lunar", holiday);
                    } else {
                        map.put("lunar", CalendarChange.CalendarChange(tempStr));
                    }
                    if (isRed) {
                        map.put("isHoliday", "1"); // 1节日
                        isRed = false;
                    } else {
                        map.put("isHoliday", "0"); // 1节日
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                data.add(map);
            }
        }
        if (temp_month >= 12) {
            temp_year++;
            temp_month = 1;
        } else {
            temp_month++;
        }
        int mm = data.size();
        for (int i = 1; i <= 42 - mm; i++) {
            String tempStr = temp_year + "-"
                    + (temp_month > 9 ? "" + temp_month : "0" + temp_month)
                    + "-" + (i > 9 ? "" + i : "0" + i);
            map = new HashMap<String, String>();
            map.put("day", i + "");
            map.put("time", tempStr);
            map.put("isToday", "0"); // 是今天
            map.put("isMonth", "0"); // 1是本月
            try {
                if (CalHoliday(Integer.valueOf("" + temp_month
                        + (i > 9 ? "" + i : "0" + i)))) {
                    map.put("lunar", holiday);
                } else {
                    map.put("lunar", CalendarChange.CalendarChange(tempStr));
                }
                if (isRed) {
                    map.put("isHoliday", "1"); // 1节日
                    isRed = false;
                } else {
                    map.put("isHoliday", "0"); // 1节日
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            data.add(map);
        }
        return data;
    }

    public boolean CalHoliday(int t) {
        boolean is = false;
        switch (t / 100) {
            case 2:
                if (t == solartime[0]) {
                    holiday = "立春";
                    is = true;
                } else if (t == solartime[1]) {
                    holiday = "雨水";
                    is = true;
                }
                break;
            case 3:
                if (t == solartime[2]) {
                    holiday = "惊蛰";
                    is = true;
                } else if (t == solartime[3]) {
                    holiday = "春分";
                    is = true;
                }
                break;
            case 4:
                if (t == solartime[4]) {
                    holiday = "清明";
                    is = true;
                } else if (t == solartime[5]) {
                    holiday = "谷雨";
                    is = true;
                }
                break;
            case 5:
                if (t == solartime[6]) {
                    holiday = "立夏";
                    is = true;
                } else if (t == solartime[7]) {
                    holiday = "小满";
                    is = true;
                }
                break;
            case 6:
                if (t == solartime[8]) {
                    holiday = "芒种";
                    is = true;
                } else if (t == solartime[9]) {
                    holiday = "夏至";
                    is = true;
                }
                break;
            case 7:
                if (t == solartime[10]) {
                    holiday = "小暑";
                    is = true;
                } else if (t == solartime[11]) {
                    holiday = "大暑";
                    is = true;
                }
                break;
            case 8:
                if (t == solartime[12]) {
                    holiday = "立秋";
                    is = true;
                } else if (t == solartime[13]) {
                    holiday = "处暑";
                    is = true;
                }
                break;
            case 9:
                if (t == solartime[14]) {
                    holiday = "白露";
                    is = true;
                } else if (t == solartime[15]) {
                    holiday = "秋分";
                    is = true;
                }
                break;
            case 10:
                if (t == solartime[16]) {
                    holiday = "寒露";
                    is = true;
                } else if (t == solartime[17]) {
                    holiday = "霜降";
                    is = true;
                }
                break;
            case 11:
                if (t == solartime[18]) {
                    holiday = "立冬";
                    is = true;
                } else if (t == solartime[19]) {
                    holiday = "小雪";
                    is = true;
                }
                break;
            case 12:
                if (t == solartime[20]) {
                    holiday = "大雪";
                    is = true;
                } else if (t == solartime[21]) {
                    holiday = "冬至";
                    is = true;
                }
                break;
            case 1:
                if (t == solartime[22]) {
                    holiday = "小寒";
                    is = true;
                } else if (t == solartime[23]) {
                    holiday = "大寒";
                    is = true;
                }
                break;
        }
        switch (t) {
            case 101:
                holiday = "元旦";
                isRed = true;
                is = true;
                break;
            case 214:
                holiday = "情人节";
                is = true;
                break;
            case 308:
                holiday = "妇女节";
                is = true;
                break;
            case 312:
                holiday = "植树节";
                is = true;
                break;
            case 315:
                holiday = "消费日";
                is = true;
                break;
            case 401:
                holiday = "愚人节";
                is = true;
                break;
            // case 407:
            // holiday="卫生日";
            // is=true;
            // break;
            case 501:
                holiday = "劳动节";
                is = true;
                break;
            case 504:
                holiday = "青年节";
                is = true;
                break;
            // case 512:
            // holiday="护士节";
            // is=true;
            // break;
            // case 513:
            // holiday="助残日";
            // is=true;
            // break;

            case 601:
                holiday = "儿童节";
                is = true;
                break;
            case 605:
                holiday = "世界环境日";
                is = true;
                break;
            case 701:
                holiday = "建党节";
                is = true;
                break;
            case 801:
                holiday = "建军节";
                is = true;
                break;
            case 910:
                holiday = "教师节";
                is = true;
                break;
            case 1001:
                holiday = "国庆节";
                is = true;
                break;
            case 1225:
                holiday = "圣诞节";
                is = true;
                break;
            default:
                // is=false;
                break;
        }

        return is;
    }

    @SuppressWarnings("deprecation")
    public void GenView() {
        dataLinear.removeAllViews();
        mLayoutParamss.weight = 1;
        LayoutInflater lif = LayoutInflater.from(context);
        LinearLayout temp = new LinearLayout(context);
        int size = data.size();
        for (int i = 0; i < size; i++) {
            View convertView = lif.inflate(R.layout.gritem, null);
            TextView time = (TextView) convertView.findViewById(R.id.time);
            TextView lunar = (TextView) convertView.findViewById(R.id.lunar);
            TextView fraction = (TextView) convertView.findViewById(R.id.fraction);
            LinearLayout itemroot = (LinearLayout) convertView.findViewById(R.id.itemroot);
            HashMap<String, String> map = data.get(i);
            if ((i + 1) < size) {
                HashMap<String, String> map2 = data.get(i + 1);
                if (map2.get("lunar").equals("春节")) {
                    map.put("lunar", "除夕");
                }
            }
            if (map.get("isMonth").equals("1"))// 1是本月
            {
                itemroot.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_riliactivity));
                if (map.get("isToday").equals("1"))// 1是今天
                {
                    time.setTextColor(context.getResources().getColor(R.color.sunday_txt));
                    lunar.setTextColor(context.getResources().getColor(R.color.sunday_txt));
//                    itemroot.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_riliactivity_jintian));
                } else {
                    itemroot.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_riliactivity));
                }
//                int m = (i + 1) % 7;
//                if (m == 1 || m == 0) {
//                    time.setTextColor(Color.rgb(50, 79, 133));
//                }
                if (map.get("isHoliday").equals("1")) // 节日
                {
                    time.setTextColor(Color.RED);
                }
                itemroot.setTag(R.id.calendar_index, "1");//0其他,1是本月
            } else {
                time.setTextColor(context.getResources().getColor(R.color.othermonth_txt));
                lunar.setTextColor(context.getResources().getColor(R.color.othermonth_txt));
                itemroot.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_riliactivity));
                itemroot.setTag(R.id.calendar_index, "0");
            }
            String day = map.get("day");
            String datestr = standard_year + "-" +
                    (standard_month > 9 ? standard_month : "0" + standard_month) + "-" +
                    (day.length() > 1 ? day : "0" + day);
            if (map.get("isMonth").equals("1")) {// 1是本月
                if (App.getDBcApplication().queryISNOTHOLIDAY(datestr).equals("1")) {//休息
                    fraction.setText("休");
                    fraction.setTextColor(context.getResources().getColor(R.color.sunday_txt));
                } else if (App.getDBcApplication().queryISNOTHOLIDAY(datestr).equals("2")) {//上班
                    fraction.setText("班");
                    fraction.setTextColor(context.getResources().getColor(R.color.title0));
                } else {
                    fraction.setText("");
                }
            }
            if (!map.get("isMonth").equals("1")) {
                time.setVisibility(View.GONE);
                fraction.setVisibility(View.GONE);
                lunar.setVisibility(View.GONE);
            }
            time.setText(day);
            lunar.setText(map.get("lunar"));
            itemroot.setTag(day);
            itemroot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String day = v.getTag().toString();
                    // 0其他,1是本月
                    String calendar_index = v.getTag(R.id.calendar_index).toString();
                    if ("1".equals(calendar_index)) {
                        Intent intent = null;
                        String dateTime = Integer.toString(standard_year)
                                + "-"
                                + (standard_month > 9 ? "" + standard_month
                                : "0" + standard_month) + "-"
                                + (Integer.parseInt(day) > 9 ? day : "0" + day);
                        Message message = new Message();
                        message.what = 9;
                        message.obj = dateTime;
                        handlerrili.sendMessage(message);
                    }
                }
            });
            if ((i + 1) % 7 != 0) {
                temp.addView(convertView, mLayoutParamss);
            } else {
                if (i != 0) {
                    temp.addView(convertView, mLayoutParamss);
                    dataLinear.addView(temp, mLayoutParamss);
                    temp = new LinearLayout(context);
                }
            }
        }
    }

    /*
     * 基姆拉尔森计算公式 QgW4jIbx W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 /
     *
     * d 天 m 月 y 年 1月2月换算为去年的13 14月计算
     *
     * 基姆拉尔森计算公式的C#的实现 %NfbgJcL_ //y：年，m：月，d：日。在参数都只传入相应的整数
     */
    private int calWeek(int y, int m, int d) {
        int a = 7; // 用来保存计算得到的星期几的整数 Dz>v;%$S-
        if ((m == 1) || (m == 2))// 如果是一月或二月进行换算 uWKc .
        {
            m += 12;
            y--;
        }
        a = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; // 得到的星期几的整数
        // 
        // 7ehs+GI
        a++;
        if (a == 7) {
            a = 0;
        }
        return a;
    }

    public void calLeapYear(int i) {
        if (i % 100 == 0) {
            if (i % 400 == 0) {
                isLeap = true;
            } else {
                isLeap = false;
            }
        } else {
            if (i % 4 == 0) {
                isLeap = true;
            } else {
                isLeap = false;
            }
        }
    }

    private void dialogMorningTimeOnClick(TextView time_tv) {
        final BaseDialog dialog = new BaseDialog(context, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
//        params.alpha = 0.92f;
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(context);
        View more_pop_menu = fac.inflate(R.layout.dialog_gundong_time, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = windowManager.getDefaultDisplay().getWidth();
        dialog.show();
        dialog.setCallBack(new BaseDialog.CallBack() {
            @Override
            public void onTouchOutside() {
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CLOCKORDER, "0");
                dialog.dismiss();
            }
        });
        new TimeOnClick(dialog, more_pop_menu, time_tv);
    }

    class TimeOnClick {

        private View view;
        private Dialog dialog;
        // private PickerView hour_pv;
        // private PickerView minute_pv;
        CycleWheelView hour_cy;
        CycleWheelView min_cy;
        String hour;
        String minute;
        private String timeSet = "";
        private TextView time_tv;

        Map<String, String> mMap;
        ToggleButton toggleButton;

        @SuppressLint("NewApi")
        public TimeOnClick(Dialog dialog, View view, TextView time_tv) {
            this.dialog = dialog;
            this.view = view;
            this.time_tv = time_tv;
            timeSet = schTime;
            initview();
            initdata();
        }

        private void initview() {
            hour_cy = (CycleWheelView) view.findViewById(R.id.hour_cy);
            min_cy = (CycleWheelView) view.findViewById(R.id.min_cy);

//            view.findViewById(R.id.bp).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    dialog.dismiss();
//                    initDiaLog(time_tv);
//                }
//            });

            toggleButton = (ToggleButton) view.findViewById(R.id.toggle);
            if (schDisplayTime == 0) {
                toggleButton.setChecked(true);
            } else {
                toggleButton.setChecked(false);
            }
            toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked) {
                    if (isChecked) {
                        week_date.setText(date_str + " " + DateUtil.Week2(date_str) + " 全天");
                        schDisplayTime = 0;
                        isAlarm = 0;
                    } else {
                        schDisplayTime = 1;
                        isAlarm = 1;
                        week_date.setText(date_str + " " + DateUtil.Week2(date_str) + " "+timeSet);
                    }
                }
            });
        }

        private void initdata() {
            int curHours = Integer.parseInt(timeSet.split(":")[0]);
            int curMinutes = Integer.parseInt(timeSet.split(":")[1]);
            hour = curHours + "";
            minute = curMinutes + "";
            int currentHoursIndex = 0;
            int currentMinutesIndex = 0;
            List<String> hours = new ArrayList<String>();
            List<String> minutes = new ArrayList<String>();
            for (int i = 0; i < 24; i++) {
                hours.add(i < 10 ? "0" + i : "" + i);

            }
            for (int i = 0; i < 60; i++) {
                minutes.add(i < 10 ? "0" + i : "" + i);
            }
            for (int i = 0; i < hours.size(); i++) {
                if (curHours == Integer.parseInt(hours.get(i))) {
                    currentHoursIndex = i;
                }
            }
            for (int i = 0; i < minutes.size(); i++) {
                if (curMinutes == Integer.parseInt(minutes.get(i))) {
                    currentMinutesIndex = i;
                }
            }
            hour_cy.setLabels(hours);
            try {
                hour_cy.setWheelSize(5);
            } catch (CycleWheelView.CycleWheelViewException e) {
                e.printStackTrace();
            }
            hour_cy.setCycleEnable(true);
            hour_cy.setSelection(currentHoursIndex);
            hour_cy.setAlphaGradual(0.6f);
            hour_cy.setDivider(context.getResources().getColor(R.color.gongkai_txt), 1);
            hour_cy.setSolid(Color.WHITE, Color.WHITE);
            hour_cy.setLabelColor(context.getResources().getColor(R.color.gongkai_txt));
            hour_cy.setLabelSelectColor(Color.BLACK);
            hour_cy.setOnWheelItemSelectedListener(new CycleWheelView.WheelItemSelectedListener() {
                @Override
                public void onItemSelected(int position, String label) {
                    hour = Integer.parseInt(label) < 10 ? "0"
                            + Integer.parseInt(label) : label;
                    minute = Integer.parseInt(minute) < 10 ? "0"
                            + Integer.parseInt(minute) : minute;

                    if (!timeSet.equals(hour + ":" + minute)) {
                        toggleButton.setChecked(false);
                    }
                    schTime = timeSet = hour + ":" + minute;
                    if (toggleButton.isChecked() == false) {
                        isAlarm = 1;
                        schDisplayTime = 1;
                        week_date.setText(date_str + " " + DateUtil.Week2(date_str) + " "+timeSet);
                    }
                }
            });

            min_cy.setLabels(minutes);
            try {
                min_cy.setWheelSize(5);
            } catch (CycleWheelView.CycleWheelViewException e) {
                e.printStackTrace();
            }
            min_cy.setCycleEnable(true);
            min_cy.setSelection(currentMinutesIndex);
            min_cy.setAlphaGradual(0.6f);
            min_cy.setDivider(context.getResources().getColor(R.color.gongkai_txt), 1);
            min_cy.setSolid(Color.WHITE, Color.WHITE);
            min_cy.setLabelColor(context.getResources().getColor(R.color.gongkai_txt));
            min_cy.setLabelSelectColor(Color.BLACK);
            min_cy.setOnWheelItemSelectedListener(new CycleWheelView.WheelItemSelectedListener() {
                @Override
                public void onItemSelected(int position, String label) {
                    hour = Integer.parseInt(hour) < 10 ? "0"
                            + Integer.parseInt(hour) : hour;
                    minute = Integer.parseInt(label) < 10 ? "0"
                            + Integer.parseInt(label) : label;
                    if (!timeSet.equals(hour + ":" + minute)) {
                        toggleButton.setChecked(false);
                    }
                    schTime = timeSet = hour + ":" + minute;
                    if (toggleButton.isChecked() == false) {
                        isAlarm = 1;
                        schDisplayTime = 1;
                        week_date.setText(date_str + " " + DateUtil.Week2(date_str) + " "+timeSet);
                    }
                }
            });
        }
    }
}
